<template>
  <view-box ref="viewBox" body-padding-bottom="55px" class="contaioner" id="box">
    <div class="chat-box" id="chat">
      <template v-for="item in list" >
        <div class="chat-sender" v-if="item.from_user_id === from">
          <div>
            <img :src="item.from.avatar" v-if="item.from.avatar.indexOf('http:') !== -1">
            <img :src="$apiUrl + '/storage/' + item.from.avatar" v-else>
          </div>
          <div>{{item.from.name}} <span class="time">{{item.created_at.substr(5, 11)}}</span></div>
          <div>
            <div class="chat-left_triangle"></div>
            <span v-html="item.body"></span>
          </div>
        </div>
        <!-- Right -->
          <div class="chat-receiver" v-if="item.from_user_id === userInfo.id">
            <div>
              <img :src="item.from.avatar" v-if="item.from.avatar.indexOf('http:') !== -1">
              <img :src="$apiUrl + '/storage/' + item.from.avatar" v-else>
            </div>
            <div><span class="time">{{item.created_at.substr(5, 11)}}</span> {{item.from.name}}</div>
            <div>
              <div class="chat-right_triangle"></div>
              <span v-html="item.body"></span>
            </div>
          </div>
        </template>
      <!-- Notice/Center -->
      <!-- <div class="chat-notice">
        <span>2017年12月10日 23:13</span>
      </div> -->
      <!-- Notice/Center -->
      <!-- <div class="chat-notice">
        <span>你被群主移除群聊</span>
      </div> -->
    </div>
    <div class="search">
      <input type="file" @change="chat()" name="thumb" accept="image/gif,image/jpeg,image/jpg,image/png" style="display:none" ref="uploadImg">
       <group>
          <x-input class="weui-chat" v-model="msg" :show-clear="false">
            <div slot="label" @click="upload()">
              <i class="iconfont icon-font29"></i>
            </div>
            <div slot="right" >
              <x-button type="primary" :disabled="!msg || sending" :show-loading="sending" @click.native="chat()" mini>发送</x-button>
            </div> 
          </x-input>
      </group>
    </div>
  </view-box>
</template>
<script>
  import { ViewBox, Search, Group, XInput, XButton } from 'vux'
  export default {
    data () {
      return {
        sending: false,
        msg: null,
        from: 0,
        list: [],
        source: null
      }
    },
    components: {
      ViewBox,
      Search,
      Group,
      XInput,
      XButton
    },
    computed: {
      userInfo () {
        return this.$store.getters.userInfo
      }
    },
    watch: {
      userInfo () {
        this._getChatRecords()
      },
      list: {
        deep: true,
        handler (newValue) {
          document.getElementById('chat').scrollTop = document.getElementById('chat').height
        }
      }
    },
    updated () {
      this.$nextTick(function () {
        // document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight
      })
    },
    mounted () {
      this.from = parseInt(this.$route.params.user)
      this._getChatRecords()
    },
    methods: {
      chat () {
        if (!this.from || !this.userInfo) {
          return false
        }
        let formData = new FormData()
        if (this.$refs.uploadImg.files.length !== 0) {
          formData.append('file', this.$refs.uploadImg.files[0])
        }
        formData.append('to', this.from)
        formData.append('from', this.userInfo.id)
        formData.append('body', this.msg)
        this.sending = true
        this.$http.post('/chats', formData).then((response) => {
          this.$refs.uploadImg.value = ''
          this.msg = null
          this.sending = false
          this.list.push(response.data)
        })
      },
      upload () {
        this.$refs.uploadImg.click()
      },
      _getChatRecords () {
        if (!this.userInfo) {
          return false
        }
        let _this = this
        this.source = new EventSource(this.$apiUrl + '/api/chats?current_user=' + this.userInfo.id + '&with_user=' + this.from)
        this.source.onmessage = function (e) {
          _this.list = JSON.parse(e.data)
        }
      }
    },
    destroyed () {
      if (this.source) {
        this.source.close()
      }
    }
  }
</script>
<style scoped>
 .contaioner {
  background-color: #ebebeb;
  font-family: -apple-system;
  font-family: "-apple-system", "Helvetica Neue", "Roboto", "Segoe UI", sans-serif;
}
.chat-sender{
  clear:both;
  font-size: 16px;
}
.chat-sender div:nth-of-type(1){
  float: left;
}
.chat-sender div:nth-of-type(2){
  margin: 0 50px 2px 50px;
  padding: 0px;
  color: #848484;
  font-size: 70%;
  text-align: left;
}
.chat-sender div:nth-of-type(3){
  background-color: white;
  /*float: left;*/
  margin: 0 50px 10px 50px;
  padding: 10px 10px 10px 10px;
  border-radius:7px;
  text-indent: -12px;
}

.chat-receiver{
  clear:both;
  font-size: 16px;
}
.chat-receiver div:nth-of-type(1){
  float: right;
}
.chat-receiver div:nth-of-type(2){
  margin: 0px 50px 2px 50px;
  padding: 0px;
  color: #848484;
  font-size: 70%;
  text-align: right;
}
.chat-receiver div:nth-of-type(3){
  /*float:right;*/
  background-color: #b2e281;
  margin: 0px 50px 10px 50px;
  padding: 10px 10px 10px 10px;
  border-radius:7px;
}

.chat-receiver div:first-child img,
.chat-sender div:first-child img{
  width: 40px;
  height: 40px;
  /*border-radius: 10%;*/
}

.chat-left_triangle{
  height: 0px;
  width: 0px;
  border-width: 6px;
  border-style: solid;
  border-color: transparent white transparent transparent;
  position: relative;
  left: -22px;
  top: 3px;
}
.chat-right_triangle{
  height: 0px;
  width: 0px;
  border-width: 6px;
  border-style: solid;
  border-color: transparent transparent transparent #b2e281;
  position: relative;
  right:-22px;
  top:3px;
}
.time {
  color: #cecece;
}
.chat-notice{
  clear: both;
  font-size: 70%;
  color: white;
  text-align: center;
  margin-top: 15px;
  margin-bottom: 15px;
}
.chat-notice span{
  background-color: #cecece;
  line-height: 25px;
  border-radius: 5px;
  padding: 5px 10px;
}
.search {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}
.chat-box {
  padding: 10px;
}
.weui-chat .iconfont {
  padding-right: 10px;
  font-size: 20px;
  color: #666;
  border-right: 1px solid #999;
  margin-right: 10px;
}
</style>